import { Schema } from "mongoose";
import type Typings from "../typings";

import { createDb } from "../mongoose/mongoose";
const db = createDb("service", "role");

// 定义 Schema 模型
const RoleSchema = new Schema<Typings.Role.props>({
  id: {
    type: String,
    required: true,
    unique: true,
  },

  roleName: {
    type: String,
    required: true,
  },

  roleId: {
    type: String,
    required: true,
  },

  permissionKey: {
    type: [String],
    required: true,
  },

  halfPermissionKey: {
    type: [String],
    required: true,
    default: [],
  },

  permissionButtonKeys: {
    type: Array,
    default: []
  },

  createdTime: {
    type: Number,
    Number,
    required: true,
    default: Date.now(),
  },

  updatedTime: {
    type: Number,
    required: true,
    default: Date.now(),
  },

  creatorId: {
    type: String,
    required: true,
    default: "",
  },
});

// 排除 _id 和 __v 字段
RoleSchema.set("toObject", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

RoleSchema.set("toJSON", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

const RoleModel = db.model<Typings.Role.props>("Role", RoleSchema, "roles");

export default RoleModel;
